# -*- coding: utf-8 -*-

if request.env.web2py_runtime_gae:
    db = DAL('google:datastore')
    session.connect(request, response, db = db)
else:
    from gluon.custom_import import track_changes
    track_changes()
    try:
        from connect_string import account
        db = DAL('oracle://' + account, check_reserved=['oracle'])
    except:
        db = DAL('sqlite://storage.sqlite', check_reserved=['oracle'])

# by default give a view/generic.extension to all actions from localhost
# none otherwise. a pattern can be 'controller/function.extension'
response.generic_patterns = ['*'] if request.is_local else []

from gluon.tools import Mail, Auth, Crud, Service, PluginManager, prettydate
mail = Mail()                                  # mailer
auth = Auth(db)                                # authentication/authorization
crud = Crud(db)                                # for CRUD helpers using auth
service = Service()                            # for json, xml, jsonrpc, xmlrpc, amfrpc
plugins = PluginManager()                      # for configuring plugins

mail.settings.server = 'logging' or 'smtp.gmail.com:587'  # your SMTP server
mail.settings.sender = 'you@gmail.com'         # your email
mail.settings.login = 'username:password'      # your credentials or None

##
# Local mods for auth table and CAS use when on pc.cs.purdue.edu (jtk)
#
import socket
USE_CAS = socket.gethostname() == 'pc.cs.purdue.edu'

db.define_table(
    auth.settings.table_user_name,
    Field('username', length=15, writable=not USE_CAS),
    Field('handle', length=15, writable=True),
    Field('first_name', length=128, default=''),
    Field('last_name', length=128, default=''),
    Field('password', 'password', length=128, readable=False, label='Password', writable=not USE_CAS),
    Field('picture', 'upload'),
    Field('publish', 'boolean', default=False),
    Field('registration_key', length=512, writable=False, readable=False, default=''),
    Field('reset_password_key', length=512, writable=False, readable=False, default=''),
    Field('registration_id', length=512, writable=False, readable=False, default=''),
    format='%(first_name)s %(last_name)s (%(handle)s)')
 
db.auth_user.username.requires = IS_NOT_IN_DB(db, db.auth_user.username)
db.auth_user.handle.requires = IS_NOT_IN_DB(db, db.auth_user.handle)

auth.settings.hmac_key = 'sha512:426f355e-fb61-40eb-b772-0397cfbc56e0'   # before define_tables()

if USE_CAS:
    from gluon.contrib.login_methods.cas_auth import CasAuth
    auth.define_tables(username=True)
    auth.settings.login_form=CasAuth(
        urlbase = "https://www.purdue.edu/apps/account/cas",
        actions = ['login','serviceValidate','logout'],
        casversion = 2,
        casusername = "cas:user")
    auth.settings.actions_disabled.append('change_password')
    auth.settings.actions_disabled.append('register')
    auth.settings.actions_disabled.append('request_reset_password')
    auth.settings.actions_disabled.append('retrieve_username')
else:
    auth.define_tables()                           # otherwise, use standard (local) Auth
#
# End of local mods for CAS authentication (jtk)
##

auth.settings.mailer = mail                    # for user email verification
auth.settings.registration_requires_verification = False
auth.settings.registration_requires_approval = False
auth.messages.verify_email = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['verify_email'])+'/%(key)s to verify your email'
auth.settings.reset_password_requires_verification = True
auth.messages.reset_password = 'Click on the link http://'+request.env.http_host+URL('default','user',args=['reset_password'])+'/%(key)s to reset your password'

crud.settings.auth = None        # =auth to enforce authorization on crud
